/*
 * @lc app=leetcode id=199 lang=cpp
 *
 * [199] Binary Tree Right Side View
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        if (!root) {
            return {};
        }
        vector<int> result;
        deque<TreeNode *> *dq = new deque<TreeNode *> ;
        deque<TreeNode *> *qnext = new deque<TreeNode *>;

        int idx = 0;
        dq->push_back(root);
        while(dq->size() > 0) {
            TreeNode *node = NULL;
            while(dq->size() > 0) {
                node = dq->front();
                dq->pop_front();
                if (node->left) qnext->push_back(node->left);
                if (node->right) qnext->push_back(node->right);
            }
            result.push_back(node->val);
            std::swap(dq, qnext);
        }
        delete dq;
        delete qnext;
        return result;
    }
};

